﻿using HotelManager.Model;
using System.Data;

namespace HotelManager.DAL
{
    public class DishListService : IDishListService
    {
        private readonly HotelDbContext _context;

        public DishListService(HotelDbContext context)
        {
            _context = context;
        }


        /// <summary>
        /// 查询所有餐饮
        /// </summary>
        /// <returns></returns>
        public List<DishList> GetList()
        {
            return _context.DishLists.ToList();            
        }

        /// <summary>
        /// 更新餐饮支出
        /// </summary>
        /// <param name="price"></param>
        /// <returns></returns>
        public int InsertPrice(int roomId, decimal price)
        {
            //string sql = "update GuestRecord set DishPrice= dishPrice+@dishPrice where RoomId=@roomId and ResideID=1";
            var guest = _context.GuestRecords.FirstOrDefault(g => g.RoomId == roomId && g.ResideId == 1);
            if (guest != null)
            {
                guest.DishPrice += price;
                _context.SaveChanges();
                return 1; // 成功
            }
            return 0; // 失败，未找到对应的记录
        }

        /// <summary>
        /// 更新餐饮信息
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public int UpdateInfo(DishList list)
        {
            //string sql = "update DishList set DishName=@dishName,Unit=@unit,Price=@price where DishId=@dishId";
            var existingDish = _context.DishLists.Find(list.DishId);
            if (existingDish == null)
            {
                return 0; // 未找到对应的记录
            }
            // 更新餐饮信息
            existingDish.DishName = list.DishName;
            existingDish.Unit = list.Unit;
            existingDish.Price = list.Price;
            _context.SaveChanges();
            return 1; // 成功
        }

        /// <summary>
        /// 新增餐饮信息
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public int InsertInfo(DishList list)
        {
            //string sql = "insert into DishList values(@dishName,@unit,@price)";
            var newDish = new DishList
            {
                DishName = list.DishName,
                Unit = list.Unit,
                Price = list.Price
            };
            _context.DishLists.Add(newDish);
            _context.SaveChanges();
            return 1;
        }

        /// <summary>
        /// 删除餐饮信息
        /// </summary>
        /// <param name="dishId"></param>
        /// <returns></returns>
        public int DelDishInfo(int dishId)
        {
            var dish = _context.DishLists.FirstOrDefault(s=>s.DishId == dishId);
            if (dish == null)
            {
                return 0; // 未找到对应的记录
            }
            _context.DishLists.Remove(dish);
            _context.SaveChanges();
            return 1; // 成功
        }

        /// <summary>
        /// 根据名称查询信息
        /// </summary>
        /// <param name="dishName"></param>
        /// <returns></returns>
        public List<DishList> FindList(string dishName)
        {
            var list = _context.DishLists
                .Where(d => d.DishName.Contains(dishName))
                .ToList();
            return list;
        }
    }
}
